package zhh.tangbao.websocket;

import cn.hutool.core.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import zhh.tangbao.websocket.pojo.WebSocketMessageEntity;
import zhh.tangbao.websocket.service.WebSocketService;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *     描述
 * </p>
 * @author 粥灰灰
 * <p>2023/4/20</p>
 */

@Component
@Slf4j
public class WebSocketSendMessageService {

    @Autowired
    private Map<String, WebSocketService> map;

    public void sendMessageToOne(String sendUserId, String receiverId, WebSocketMessageEntity message) {
        try {
            String messageType = message.getMessageType();
            WebSocketService service = map.get(messageType);
            Assert.notNull(service, "该功能未实现");
            service.sendMessageToOne(sendUserId, receiverId, message);
        } catch (Exception e) {
            log.error("消息发送失败: {}", e.getMessage());
        }
    }

    public void sendMessageToMany(String sendUserId, List<String> receiverIds, WebSocketMessageEntity message) {
        try {
            String messageType = message.getMessageType();
            WebSocketService service = map.get(messageType);
            Assert.notNull(service, "该功能未实现");
            service.sendMessageToMany(sendUserId, receiverIds, message);
        } catch (Exception e) {
            log.error("消息发送失败: {}", e.getMessage());
        }
    }

    public void sendMessageToAll(String sendUserId, WebSocketMessageEntity message) {
        try {
            String messageType = message.getMessageType();
            WebSocketService service = map.get(messageType);
            Assert.notNull(service, "该功能未实现");
            service.sendMessageToAll(sendUserId, message);
        } catch (Exception e) {
            log.error("消息发送失败: {}", e.getMessage());
        }
    }
}
